let word_data=[];
let all_target_content=[];
let all_evaluation_content=[];
let courseName;
        function generate() {
             let that = this;
                // 读取并获得模板文件的二进制内容
                JSZipUtils.getBinaryContent("../word/target_model.docx", function(error, content) {
                  // model.docx是模板。我们在导出的时候，会根据此模板来导出对应的数据
                  // 抛出异常
                  if (error) {
                    throw error;
                  }
                  word_data=[];
                  all_target_content=[];
                  all_evaluation_content=[];
                  getworddata();
                  // 创建一个PizZip实例，内容为模板的内容
                  let zip = new PizZip(content);
                  // 创建并加载docxtemplater实例对象
                  let doc = new docxtemplater().loadZip(zip);
                  // 设置模板变量的值
                  doc.setData({"text":"表 课程目标及达成目标的教学环节及评价环节的设计",
                      "table1":word_data,
                      "target":"各教学环节安排",
                      "targettext":"各教学环节的安排及具体要求如下：",
                      "alltargetcontent":all_target_content,
                      "evaluation":"考核评价方法及其基本要求",
                      "evaluationtext":"各部分的评价方式的细则如下：",
                      "allevaluationcontent":all_evaluation_content
                  });
            
                  try {
                    // 用模板变量的值替换所有模板变量
                    doc.render();
                  } catch (error) {
                    // 抛出异常
                    let e = {
                      message: error.message,
                      name: error.name,
                      stack: error.stack,
                      properties: error.properties
                    };
                    console.log(JSON.stringify({ error: e }));
                    throw error;
                  }
            
                  // 生成一个代表docxtemplater对象的zip文件（不是一个真实的文件，而是在内存中的表示）
                  let out = doc.getZip().generate({
                    type: "blob",
                    mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                  });
                  // 将目标文件对象保存为目标类型的文件，并命名
                  saveAs(out, "教学环节和评价方式表.docx");
                });
        }

        function getworddata(){
            $.ajax({
                url:'/method/findById',
                type:'get',
                dataType: 'json',
                async:false,
                contentType:'application/json',
                data:{"courseId":courseId},
                success:function (data){
                    courseName=data.courseName;
                    $.ajax({
                        url:'/target/findTargetAllById',
                        type: 'get',
                        dataType: 'json',
                        async:false,
                        data: {"courseId": courseId},
                        success:function (data){
                            let teacheWay;

                            let evaluationWay;

                            let evaluationValue;

                            for (let i in data) {
                                let teacheWayArr=[];
                                let evaluationWayArr=[];
                                let evaluationValueArr=[];

                                teacheWay = data[i].teacheWay.split('&');
                                evaluationWay = data[i].evaluationWay.split('&');
                                evaluationValue = data[i].evaluationValue.split(';');

                                for(let j in teacheWay)
                                    teacheWayArr.push({"teacheWay":teacheWay[j]});
                                for (let j in evaluationWay)
                                    evaluationWayArr.push({"evaluationWay":evaluationWay[j]});
                                for (let j in evaluationValue)
                                    if(evaluationValue[j])
                                    evaluationValueArr.push({"evaluationValue":evaluationValue[j]});

                                word_data.push({"courseTargetContent":data[i].courseTargetContent,"teacheWayList":teacheWayArr,"evaluationWayList":evaluationWayArr,"evaluationValueList":evaluationValueArr});
                            }
                            $.ajax({
                                url: '/target/findTargetContent',
                                type: 'get',
                                dataType: 'json',
                                async: false,
                                data: {"courseId": courseId},
                                success: function (data) {
                                    for (let i=0;i<data.length;i++) {
                                        let j=i+1;
                                        all_target_content.push({"courseTarget":"（"+j+"）"+data[i].courseTarget,"courseTargetContent":data[i].courseTargetContent});
                                    }
                                }
                            });
                            $.ajax({
                                url: '/target/findEvaluationContent',
                                type: 'get',
                                dataType: 'json',
                                async: false,
                                data: {"courseId": courseId},
                                success: function (data) {
                                    for (let i=0;i<data.length;i++) {
                                        let j=i+1;
                                        all_evaluation_content.push({"Evaluation":"（"+j+"）"+data[i].courseEvaluation,"EvaluationContent":data[i].courseEvaluationContent});
                                    }
                                }
                            });
                        }
                    });
                }
            })
        }